//
//  ViewController.m
//  UIColectionView
//
//  Created by apple on 15/9/25.
//  Copyright © 2015年 信徒. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
//   设置cell的默认大小
    
    flowLayout.itemSize = CGSizeMake(40 , 30);
    
//    设置滑动方向
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
//    最小行距
    flowLayout.minimumInteritemSpacing = 5;
    
    
//   最小列距
    
    flowLayout.minimumLineSpacing = 20;
    
//    分组与分组之间的间距
    
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    flowLayout.footerReferenceSize = CGSizeMake(40, 50);
    
    flowLayout.headerReferenceSize = CGSizeMake(50, 40);
    
    
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
   
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    
    collectionView.dataSource = self;
    
    collectionView.delegate = self;
    
    [self.view addSubview:collectionView];
    
}

//几个分组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
}


//每个分组有多少个矩形
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 120;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    //获取cell对象
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
 
    CGFloat r = (indexPath.item % 6) * 1.0 / 6;
    CGFloat g = (indexPath.item / 6) * 1.0 / (100 / 6);
    CGFloat b = sqrt(r*r + g*g);
    cell.backgroundColor = [UIColor colorWithRed:r green:g blue:b alpha:1.0];
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        header.backgroundColor = [UIColor redColor];
        
        return header;
    }
    else
    {
        
        UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
        
        footer.backgroundColor = [UIColor yellowColor];
        
        return footer;
        

    }
    
}

#pragma mark - UICollectionViewDelegate


//返回cell的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = rand() % 100;
    CGFloat heigth = rand() % 100;
    
    return CGSizeMake(width, heigth);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"当前选中的是第%ld组%ld号",indexPath.section,indexPath.item);
}

@end
